package Database;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper {
	
	private final static String dbname = "Scheduler";
	private final static String tablename = "ringtone_schedules";
	private final static int version = 2;
	private final static String id = "_id";
	private static String createDatabase ="create table if not exists ringtone_schedules(_id int primary key,strt_hrs int,strt_min  int,end_hrs int,end_min  int," +
	"ringtone_src varchar(60),profile_id int);";
	DatabaseHelper helper;
	private SQLiteDatabase SQLite;
	private Context ctx;
	
private static class DatabaseHelper extends SQLiteOpenHelper {
		
	    DatabaseHelper(Context context) { super(context, dbname, null, version); }
	
	    @Override
	    public void onCreate(SQLiteDatabase db) { db.execSQL(createDatabase); }
	
	    @Override
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { onCreate(db); }
	}
	
	
	public DBHelper(Context ctx){ this.ctx = ctx; }
	
	
	public  DBHelper openDB(){
		helper = new DatabaseHelper(ctx);
		SQLite = helper.getWritableDatabase();
		
		return this;
	}
	
	public void close() { helper.close(); }
	public int getProfilesCount()
	{
		Cursor cur=SQLite.rawQuery("select distinct profile_id from '"+tablename+"';", null);
		return cur.getCount();
	}
	public List getProfiles()
	{
		Cursor cur=SQLite.rawQuery("select distinct profile_id from ringtone_schedules ;", null);
		List<Integer> profiles_list = new ArrayList();
		cur.moveToFirst();
		int i=0;
		while(i++<cur.getCount())
		{
			profiles_list.add(cur.getInt(cur.getColumnIndex("profile_id")));
			cur.moveToNext();
			
		}
		cur.close();
		return profiles_list;	
	}
	
	public List<Ringtone> getRingtonesByProfilesId(int profile_id)
	{
		Cursor cur=SQLite.rawQuery("select * from ringtone_schedules where profile_id='"+profile_id+"';", null);
		List<Ringtone> tones_list = new ArrayList();
		cur.moveToFirst();
		int i=0;
		while(i++<cur.getCount())
		{
			int id=cur.getInt(cur.getColumnIndex("_id"));
			int strt_hrs=cur.getInt(cur.getColumnIndex("strt_hrs"));
			int strt_min=cur.getInt(cur.getColumnIndex("strt_min"));
			int end_hrs=cur.getInt(cur.getColumnIndex("end_hrs"));
			int end_min=cur.getInt(cur.getColumnIndex("end_min"));
			String src=cur.getString(cur.getColumnIndex("ringtone_src"));
			int pid=cur.getInt(cur.getColumnIndex("profile_id"));
			Ringtone r=new Ringtone(id,strt_hrs,strt_min,end_hrs,end_min,src,pid);
			tones_list.add(r);
			cur.moveToNext();
			
			
		}
		cur.close();
		return tones_list;
		
	}
	
public long setTime(int _id,int strt_hrs,int strt_min,int end_hrs,int end_min,String ringtone_src,int profile_id) 
{
		
		ContentValues values = new ContentValues();
		values.put("_id", _id);
		values.put("strt_hrs", strt_hrs);
		values.put("strt_min",strt_min);
		values.put("end_hrs", end_hrs);
		values.put("end_min",end_min);
		values.put("ringtone_src", ringtone_src);
		values.put("profile_id", profile_id);
		return SQLite.insert(tablename, null, values);
		
	}
public void editTime(int id,int strt_hrs,int strt_min,int end_hrs,int end_min,String ringtone_src,int profile_id) 
{
	String query="update "+tablename+" set strt_hrs="+strt_hrs+",strt_min="+strt_min+" ,end_hrs="+end_hrs+",end_min="+end_min+", ringtone_src='"+ringtone_src+"' , profile_id='"+profile_id+"' where _id="+id;
	SQLite.execSQL(query);
	
}

public void delete(int id)
{
	String query="delete from "+tablename+" where _id="+id;
	SQLite.execSQL(query);
	
}

public void deleteProfile(int profile_id)
{
	String query="delete from "+tablename+" where profile_id="+profile_id;
	SQLite.execSQL(query);
	
}
}
